<?php

class Zones extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		//$this->db_front = $this->load->database('front', TRUE);
		$this->load->model('zones_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		//$this->load->library('security');
		$this->load->library('auth/tank_auth');
		$this->lang->load('auth/tank_auth');
		$this->load->model('tank_auth/users');
		$this->load->library('pagination');
		$this->db_front = $this->load->database('front', TRUE);
	}
	
	function index(){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				
				$offset = $this->uri->segment(3);
				$limit = '10';
				if(!$offset){
					$page = 0;
				}else{
					$page = $offset; 	
				}
				$data['hasil'] = $this->zones_model->getAll($page,$limit);
				
				$config_pagination['base_url']    = site_url('zones/index');
				$config_pagination['total_rows']  = count($this->zones_model->getAll('0','0'));
				$config_pagination['per_page']    = $limit;
				$config_pagination['uri_segment'] = 3;
				$config_pagination['num_links']   = 4;
		
				//pagination style
				$config_pagination['full_tag_open'] = '<div class="pagination"><ul>';
				$config_pagination['full_tag_close'] = '</ul></div>';
		
				$config_pagination['first_link'] = 'First';
				$config_pagination['first_tag_open']  = '<li>';
				$config_pagination['first_tag_close'] = '</li>';
		
				$config_pagination['last_link'] = 'Last';
				$config_pagination['last_tag_open']  = '<li>';
				$config_pagination['last_tag_close'] = '</li>';
		
				$config_pagination['next_link'] = '&gt;';
				$config_pagination['next_tag_open']  = '<li>';
				$config_pagination['next_tag_close'] = '</li>';
		
				$config_pagination['prev_link'] = '&lt;';
				$config_pagination['prev_tag_open'] = '<li>';
				$config_pagination['prev_tag_close'] = '</li>';
		
				$config_pagination['cur_tag_open'] = '<li class="disabled"><a href="#">';
				$config_pagination['cur_tag_close'] = '</a></li>';
		
				$config_pagination['num_tag_open'] = '<li>';
				$config_pagination['num_tag_close'] = '</li>';
		
				$this->pagination->initialize($config_pagination); 
		
				$data['pagination'] = $this->pagination->create_links();
				
				$data['content'] = 'zones';
				$this->load->view('common/common',$data);
			}else{
					redirect('/common/');
				}
		}
		
	}
	
	function add(){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			$this->form_validation->set_rules('name','Zone Name','trim|required|xss_clean|max_length[100]');
			$this->form_validation->set_rules('code','Zone Code','trim|required|xss_clean|max_length[30]');
			$this->form_validation->set_rules('country_id','Country','trim|required|xss_clean');
			$this->form_validation->set_rules('status','Status','trim|required|xss_clean');
			if ($this->tank_auth->get_url()){
				if ($this->form_validation->run() == FALSE){
					$data['content'] = 'zones_form';
					$this->load->view('common/common',$data);
				}else {
					$this->zones_model->create($_POST);
					redirect('zones/index');
				}
			}else{
					redirect('/common/');
				}
		}
	}
	
	function update($zone_id){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			$this->form_validation->set_rules('name','Zone Name','trim|required|xss_clean|max_length[100]');
			$this->form_validation->set_rules('code','Zone Code','trim|required|xss_clean|max_length[30]');
			$this->form_validation->set_rules('country_id','Country','trim|required|xss_clean');
			$this->form_validation->set_rules('status','Status','trim|required|xss_clean');
			if ($this->tank_auth->get_url()){
				if ($this->form_validation->run() == FALSE){
					$data['hasil'] =  $this->zones_model->select($zone_id);
					$data['content'] = 'zones_form_update';
					$this->load->view('common/common',$data);
				}else{
					$this->zones_model->update($zone_id);
					redirect('zones/index');
				}
			}else{
				redirect('/common/');
			}
		}
	}
	
	function delete($zone_id){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				$this->zones_model->delete($zone_id);
				redirect('zones/index');
			}else{
				redirect('/common/');
			}
		}
	}
}

?>